package org.etlt.extract;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

public class Entity implements Serializable {
    private final int index;

    private final Map<String, Object> data;

    public Entity(int index, Map<String, Object> data) {
        this.index = index;
        this.data = data;
    }

    public Object getValue(String name) {
        return data.get(name);
    }

    /**
     * return the first value, only if one value existed
     * @return
     */
    public Object getValue(){
        Collection<Object> values = data.values();
        if(values.size() != 1){
            throw new IllegalStateException("values size is not equal 1.");
        }
        return values.stream().findFirst().get();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Entity entity = (Entity) o;
        return index == entity.index && data.equals(entity.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(index, data);
    }
}
